package com.koron.order.workbench.jobStatistics.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.koron.bean.query.Page;
import com.koron.common.core.business.workflow.bean.param.ListParam;
import com.koron.common.core.business.workflow.bean.result.FlowList;
import com.koron.common.core.business.workflow.service.SysWorkflowService;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.common.util.DateUtils;
import com.koron.order.workbench.jobStatistics.bean.dto.JobStatisticsDto;
import com.koron.order.workbench.jobStatistics.bean.query.JobQuery;
import com.koron.order.workbench.jobStatistics.bean.query.JobStatisticsQuery;
import com.koron.order.workbench.jobStatistics.mapper.JobStatisticsMapper;
import com.koron.order.workbench.jobStatistics.service.JobStatisticsService;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName JobStatisticsServiceImpl
 * @description
 * @Author zhouj
 * @Date 2022/3/9 17:46
 */
@Service
public class JobStatisticsServiceImpl implements JobStatisticsService {

    @Autowired
    SysWorkflowService workflowService;

    @Override
    @TaskAnnotation("getJobStatistics")
    public List<JobStatisticsDto> getJobStatistics(SessionFactory factory, JobStatisticsQuery jobStatisticsQuery) {

        //编写 日期工具类  返回今天 本周 本月 昨天 上周 上月
        String dateType = jobStatisticsQuery.getDateType();

        JobQuery jobQuery = new JobQuery();

        String account = jobStatisticsQuery.getAccount();

        if (StringUtils.isEmpty(account)) {
            account = ThreadLocalContext.get().getAccount();
        }

        jobQuery.setAccount(account);

        switch (dateType) {
            case "day":
                jobQuery.setCurrStart(DateUtils.getTodayStart());
                jobQuery.setCurrEnd(DateUtils.getTodayEnd());
                jobQuery.setPreStart(DateUtils.getPreTodayStart());
                jobQuery.setPreEnd(DateUtils.getPreTodayEnd());
                break;
            case "week":
                jobQuery.setCurrStart(DateUtils.getWeekStart());
                jobQuery.setCurrEnd(DateUtils.getWeekEnd());
                jobQuery.setPreStart(DateUtils.getPreWeekStart());
                jobQuery.setPreEnd(DateUtils.getPreWeekEnd());
                break;
            case "month":
                jobQuery.setCurrStart(DateUtils.getMonthStart());
                jobQuery.setCurrEnd(DateUtils.getMonthEnd());
                jobQuery.setPreStart(DateUtils.getPreMonthStart());
                jobQuery.setPreEnd(DateUtils.getPreMonthEnd());
                break;
        }

        return getJobStatistics(factory, jobQuery);
    }

    public List<JobStatisticsDto> getJobStatistics(SessionFactory factory, JobQuery jobQuery) {

        List<JobStatisticsDto> jobStatisticsDtoList = new ArrayList<>();

        JobStatisticsMapper mapper = factory.getMapper(JobStatisticsMapper.class);

        //1.发现缺陷 统计自己在当前周期内创建的缺陷工单总数
        JobStatisticsDto createDefect = mapper.queryCreateDefect(jobQuery);

        //2.完成工单 统计自己是工作班成员且工单的实际完成时间在当前周期内的工单
        JobStatisticsDto finishOrder = mapper.queryFinishOrder(jobQuery);

        //3.完成巡视 统计自己是巡视人且工单的实际完成时间在当前周期内的巡视工单
        JobStatisticsDto finishPatrol = mapper.queryFinishPatrol(jobQuery);

        //4.工作票	统计自己在当前周期内创建的工作票总数
        JobStatisticsDto createTicketWork = mapper.queryCreateTicketWork(jobQuery);

        //5.操作票	统计自己在当前周期内创建的操作票总数
        JobStatisticsDto createTicketOperation = mapper.queryCreateTicketOperation(jobQuery);

        //6.累计值班 统计自己是值班成员，且交班时间在当前周期内的值班总次数
        JobStatisticsDto shiftRecord = mapper.queryShiftRecord(jobQuery);

        //7.录入台账	统计自己在当前周期内创建的设备总数
        JobStatisticsDto inputEquipment = mapper.queryInputEquipment(jobQuery);

        //8.审批流程	统计自己经手审批的流程总个数
        JobStatisticsDto workFlowAudit = new JobStatisticsDto();
        workFlowAudit.setType("workFlowAudit");
        ListParam listParam = new ListParam();
        listParam.setOperator(jobQuery.getAccount());
        listParam.setStartDate(DateUtils.dateToString(jobQuery.getCurrStart()));
        listParam.setEndDate(DateUtils.dateToString(jobQuery.getCurrEnd()));
        listParam.setType(3);
        Page<FlowList> curr_flowListPage = workflowService.listBill(listParam);
        workFlowAudit.setCurr(curr_flowListPage.getTotal());
        listParam.setStartDate(DateUtils.dateToString(jobQuery.getPreStart()));
        listParam.setEndDate(DateUtils.dateToString(jobQuery.getPreEnd()));
        Page<FlowList> pre_flowListPage = workflowService.listBill(listParam);
        workFlowAudit.setPre(pre_flowListPage.getTotal());

        jobStatisticsDtoList.add(createDefect);
        jobStatisticsDtoList.add(finishOrder);
        jobStatisticsDtoList.add(finishPatrol);
        jobStatisticsDtoList.add(createTicketWork);
        jobStatisticsDtoList.add(createTicketOperation);
        jobStatisticsDtoList.add(shiftRecord);
        jobStatisticsDtoList.add(inputEquipment);
        jobStatisticsDtoList.add(workFlowAudit);

        return jobStatisticsDtoList;
    }

    @Override
    @TaskAnnotation("orderFinishRate")
    public Map orderFinishRate(SessionFactory factory, String startTime, String endTime) {
        Map<String, Object> result = new HashMap<>();
        JobStatisticsMapper mapper = factory.getMapper(JobStatisticsMapper.class);
        JobQuery jobQuery = new JobQuery();
        jobQuery.setCurrStart(DateUtil.parse(startTime));
        jobQuery.setCurrEnd(DateUtil.parse(endTime));
        // 缺陷工单
        JobStatisticsDto defect = mapper.queryDefectFinishRate(jobQuery);
        Object defectRate = NumberUtil.div(defect.getPre(), defect.getCurr());
        result.put("defectRate", defectRate);

        // 巡视工单
        JobStatisticsDto patrol = mapper.queryPatrolFinishRate(jobQuery);
        Object patrolRate = NumberUtil.div(patrol.getPre(), patrol.getCurr());
        result.put("patrolRate", patrolRate);

        // 维护工单
        JobStatisticsDto maintain = mapper.queryMaintainFinishRate(jobQuery);
        Object maintainRate = NumberUtil.div(maintain.getPre(), maintain.getCurr());
        result.put("maintainRate", maintainRate);

        return result;
    }
}
